package com.geniusk.system.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.geniusk.common.core.JSONResult;
import com.geniusk.security.annotation.PreFuncAuth;
import com.geniusk.security.utils.SecurityUtil;
import com.geniusk.system.model.bo.model.SelectModelByIdBO;
import com.geniusk.system.model.bo.modelAction.DeleteModelActionByIdsBO;
import com.geniusk.system.model.bo.modelAction.InsertModelActionBO;
import com.geniusk.system.model.bo.modelAction.SelectModelActionByIdBO;
import com.geniusk.system.model.bo.modelAction.UpdateModelActionBO;
import com.geniusk.system.model.pojo.ModelAction;
import com.geniusk.system.model.pojo.ModelProperty;
import com.geniusk.system.service.ModelActionService;
import com.geniusk.system.service.PropertyActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模型行为管理接口
 */
@RestController
@RequestMapping("/system/modelAction")
public class ModelActionController {

    @Autowired
    private ModelActionService modelActionService;

    @Autowired
    private PropertyActionService propertyActionService;

    /**
     * ID查询模型事件列表
     */
    @PostMapping("/selectModelActionByModelId")
    @PreFuncAuth("SYS:MODEL:SELECT")
    public JSONResult selectModelActionByModelId(@Valid @RequestBody SelectModelByIdBO selectModelByIdBO) {
        List<ModelAction> modelActionList = modelActionService.selectModelActionByModelId(selectModelByIdBO.getModelId());
        Map<String, Object> result = new HashMap<>();
        result.put("list",modelActionList);
        return JSONResult.ok(result);
    }


    /**
     * ID查询模型事件
     */
    @PostMapping("/selectModelActionById")
    @PreFuncAuth("SYS:MODEL:SELECT")
    public JSONResult selectModelActionById(@Valid @RequestBody SelectModelActionByIdBO selectModelActionByIdBO) {
        ModelAction modelAction = modelActionService.selectModelActionById(selectModelActionByIdBO.getModelActionId());
        List<ModelProperty> modelPropertyList = modelAction.getProperties();
        List<String> propertyIds = new ArrayList<>();
        if(modelPropertyList != null){
            for(ModelProperty modelProperty : modelPropertyList){
                propertyIds.add(modelProperty.getModelPropertyId().toString());
            }
        }
        modelAction.setPropertyIds(propertyIds);
        Map<String, Object> result = new HashMap<>();
        result.put("modelAction",modelAction);
        return JSONResult.ok(result);
    }

    /**
     * 添加模型事件
     */
    @PostMapping("/insertModelAction")
    @PreFuncAuth("SYS:MODEL:INSERT")
    public JSONResult insertModelAction(@Valid @RequestBody InsertModelActionBO insertModelActionBO) {
        ModelAction modelAction = JSONUtil.parse(insertModelActionBO).toBean(ModelAction.class);
        Long modelActionId = IdUtil.getSnowflake().nextId();
        modelAction.setModelActionId(modelActionId);
        modelAction.setCreateBy(SecurityUtil.getUsername());
        int rows = modelActionService.insertModelAction(modelAction);
        propertyActionService.insertPropertyAction(modelActionId, insertModelActionBO.getPropertyIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 修改模型事件
     */
    @PostMapping("/updateModelAction")
    @PreFuncAuth("SYS:MODEL:UPDATE")
    public JSONResult updateModelAction(@Valid @RequestBody UpdateModelActionBO updateModelActionBO) {
        ModelAction modelAction = JSONUtil.parse(updateModelActionBO).toBean(ModelAction.class);
        modelAction.setUpdateBy(SecurityUtil.getUsername());
        int rows = modelActionService.updateModelAction(modelAction);
        Long modelActionId = modelAction.getModelActionId();
        propertyActionService.insertPropertyAction(modelActionId, updateModelActionBO.getPropertyIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 删除模型事件
     */
    @PostMapping("/deleteModelActionByIds")
    @PreFuncAuth("SYS:MODEL:DELETE")
    public JSONResult deleteModelActionByIds(@Valid @RequestBody DeleteModelActionByIdsBO deleteModelActionByIdsBO) {
        int rows = modelActionService.deleteModelActionByIds(deleteModelActionByIdsBO.getModelActionIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }
}
